package com.five.api.service.impl;

import com.five.api.controller.UserController;
import com.five.api.dao.UserMapper;
import com.five.entity.User;
import com.five.api.service.UserService;
import com.five.exceptions.BusinessException;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author: star
 * @Date: 2021-05-18 21:36
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Override
    public int updateUser(User user) {
        /**
         * Md5算法加盐加密
         */
        Object salt = ByteSource.Util.bytes("abc");
        Md5Hash md5Hash2 = new Md5Hash(user.getPassword(), salt,2);
        user.setPassword(md5Hash2.toString());
       return userMapper.updateUser(user);

    }

    @Override
    public int insUser(User user) {
        /**
         * Md5算法加盐加密
         */
        Object salt = ByteSource.Util.bytes("abc");
        Md5Hash md5Hash2 = new Md5Hash(user.getPassword(), salt,2);

        user.setPassword(md5Hash2.toString());
        user.setSalt("abc");
        logger.info("加密后的密码 password:{}",user.getPassword());
        int i = userMapper.insUser(user);
        if (i!=1){
            throw  new BusinessException("注册未成功");
        }
        return i;
    }

    @Override
    public List<User> selectUsers() {
        return userMapper.selectUsers();
    }



    @Override
    public User selectUserByRegister(Long telephone) {
        User dbUser =null;
        dbUser= userMapper.selectUser(telephone);
        return dbUser;

    }


    @Override
    public User selectUserByLogin(Long telephone) {
        User dbUser= userMapper.selectUser(telephone);
        return dbUser;
    }


    @Override
    public int UpdateUserUpload(String imgUrl, Long userId) {

        return userMapper.updateUserUpload(imgUrl,userId);
    }

    @Override
    public int UpdateUserBalance(BigDecimal balance, Long id) {
        return userMapper.updateUserBalance(balance,id);
    }

    @Override
    public BigDecimal selectUserBalance(Long id) {
        return userMapper.selectUserBalance(id);
    }


}
